<?php
/**
 * Created by PhpStorm.
 * User: 共赢岛
 * Date: 2023/12/19
 * Time: 17:03
 */

namespace App\Services\LifeCycle;


use App\Models\CarPackageOrder;
use App\Models\CarPackageOrderAttr;
use App\Models\CarPackageUsedLog;
use App\Models\User;
use App\Models\UserCar;
use Illuminate\Support\Facades\Auth;

class UserService
{
    public static function userInfo($user_id)
    {
        return User::find($user_id);
    }

    public static function carOne($user_id = 0)
    {
        if ($user_id) {
            $user = self::userInfo($user_id);
        } else {
            $user = Auth::user();
        }

        $car = UserCar::where('user_id', $user->id)->first();

        if ($car) {
            if ($car->order_id) {
                $order = OrderService::detail($car->order_id);
                $car->transact_name = $order->transact_name;
            }

            $car->name = $user->name;
            $car->phone = $user->phone;
            $car->id_card = $user->id_card;
        }

        return $car;
    }

    public static function saveUserInfo($param = [])
    {
        $user = Auth::user();
        $user_car = UserCar::where('user_id', $user->id)->first();

        $car = CarService::one($param['car_id']);

        $param['brand_id'] = $car->brand_id;

//        $param['car_license'] = json_decode($param['car_license'], true);
//        $param['car_license'] = $param['car_license'][0]['url'];

        if ($param['order_id']) {
            $order = OrderService::detail($param['order_id']);

            if (!$order) {
                return false;
            }

            $order->transact_name = $param['transact_name'];
            $order->user_phone = $param['phone'];
            $order->user_name = $param['name'];
            $order->save();
        }

        if ($user_car) {
            $user_car->car_id = $param['car_id'];
            $user_car->car_number = $param['car_number'] ?? '';
            $user_car->car_engine = $param['car_engine'];
            $user_car->car_frame = $param['car_frame'];
            $user_car->car_license = $param['car_license'] ?? '';
            $user_car->save();
        } else {
            $param['user_id'] = $user->id;
            UserCar::create($param);
        }

        $user->name = $param['name'];
        $user->id_card = $param['id_card'];
        $user->phone = $param['phone'];

        $user->save();

        return true;
    }

    public static function verifyOldUser($param)
    {
        if (!$param['phone']) {
            return failureMessage('手机号不能为空!');
        }

        $user = Auth::user();

        if (isset($param['type'])) {
            $type = $param['type'];
        } else{
            $type = 1;
        }

        $package_id = PackageService::packageTypeId($type);

        $order = CarPackageOrder::where('user_phone', $param['phone'])->whereIn('package_id', $package_id)->first();

        if (!$order) {
            return failureMessage('请联系管理员添加数据后绑定！');
        }

        if ($order->user_id) {
            return failureMessage('当前手机号已绑定套餐，不能重复绑定！');
        }

        if ($order->package_id == PACKAGE_ID_BJQ) {
            CarPackageOrderAttr::where('order_id', $order->id)->update([
                'user_id' => $user->id,
            ]);
        } else {
            $package_attr = json_decode($order->package->attrs, true);

            if ($package_attr) {
                foreach ($package_attr as $key => $value) {
                    CarPackageOrderAttr::create([
                        'user_id' => $user->id,
                        'package_id' => $order->package->id,
                        'order_id' => $order->id,
                        'package_title' => $order->package->title,
                        'attr_title' => $value['content'],
                        'price' => $value['price'],
                        'usable_number' => $value['count'],
                        'total_number' => $value['count'],
                        'used_number' => 0
                    ]);
                }
            }
        }

        $order->user_id = $user->id;
        $order->code_url = OrderService::orderCodeCreate($order->id);
        $order->code_number = OrderService::orderCodeNumber($order->id);
        $order->save();

        $user->phone = $param['phone'];
        $user->name = $order->user_name ?? $user->name;
        $user->id_card = $order->user_id_card;
        $user->save();

        return successMessage();
    }
}